package com.example.pets.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.pets.R;
import com.example.pets.model.entities.Pet;
import com.example.pets.model.entities.dao.PetDao;
import com.example.pets.model.entities.database.AppDatabase;
import com.example.pets.model.entities.database.ShareprefenseUtil;
import com.example.pets.model.entities.User;
import com.example.pets.view.activity.AuthActivity;
import com.example.pets.view.activity.EditDogProfileActivity;
import com.example.pets.view.activity.ProfileActivity;
import com.example.pets.view.fragment.adapter.PetAdapter;
import com.google.android.material.button.MaterialButton;

import java.util.ArrayList;
import java.util.List;

public class ProfileFragment extends Fragment implements View.OnClickListener{

    private View view;
    private ImageView iv_avatar;
    private TextView tv_email,tv_nickname;
    private RecyclerView rvPetList;
    private PetAdapter adapter;
    private List<Pet> petDataList;
    private User user;
    private MaterialButton btn_add_pet;
    private long uid;
    private List<Pet> list;

    public ProfileFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       view = inflater.inflate(R.layout.fragment_profile, container, false);
        iv_avatar=(ImageView) view.findViewById(R.id.iv_avatar);
        tv_nickname=(TextView) view.findViewById(R.id.tv_nickname);
        tv_email=(TextView) view.findViewById(R.id.tv_email);
        rvPetList=(RecyclerView) view.findViewById(R.id.rv_pet_list);
        btn_add_pet=(MaterialButton) view.findViewById(R.id.btn_add_pet);
        view.findViewById(R.id.btn_logout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new AlertDialog.Builder(getContext())
                        .setTitle("退出登录")
                        .setMessage("确定要退出登录吗？")
                        .setPositiveButton("确认", (dialog, which) -> logoutUser())
                        .setNegativeButton("取消", null)
                        .show();
            }
        });
        btn_add_pet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getContext(), EditDogProfileActivity.class);
                startActivity(intent);
            }
        });
        rvPetList.setLayoutManager(new LinearLayoutManager(getActivity()));

        // 初始化假数据
        petDataList = new ArrayList<>();
         uid = ShareprefenseUtil.getinstanse().getLoginUser().getUid();
        adapter = new PetAdapter(getActivity(), petDataList);
        rvPetList.setAdapter(adapter);






        view.findViewById(R.id.iv_edit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getContext(), ProfileActivity.class);
                startActivity(intent);
            }
        });



        setdata();
        return view;
    }

    private void logoutUser() {
        ShareprefenseUtil.getinstanse().logout();

        // 2. 跳转到登录页
        Intent intent = new Intent(getContext(), AuthActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        getActivity().finish();
    }


    @Override
    public void onResume() {
        super.onResume();
        setdata();
        new Thread(new Runnable() {
            @Override
            public void run() {
                PetDao petdao = AppDatabase.getDatabase(getContext()).petDao();
                list=petdao.loadAllByIds(uid);
                petDataList.clear();
                petDataList.addAll(list);
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter = new PetAdapter(getActivity(), petDataList);
                        rvPetList.setAdapter(adapter);
                    }
                });


            }
        }).start();
    }

    private void setdata() {
        user=ShareprefenseUtil.getinstanse().getLoginUser();

        Log.d("==@",user.toString());
        Glide.with(this)
                .load(user.getImageUrl())
                .into(iv_avatar);
        tv_nickname.setText(user.getUname());
        tv_email.setText(user.getEmail());
    }


    @Override
    public void onClick(View view) {

    }
}
